package com.moli.iov.admin.web.controller;

import com.moli.iov.admin.constants.Constants;
import com.moli.iov.admin.model.vo.req.SysParamSettingsQueryReqVo;
import com.moli.iov.admin.model.vo.req.SysParamSettingsReqVo;
import com.moli.iov.admin.service.SysParamSettingsService;
import com.moli.iov.base.model.dto.req.SysParamSettingsQueryReqDto;
import com.moli.iov.base.model.dto.req.SysParamSettingsReqDto;
import com.moli.iov.base.model.dto.res.SysParamSettingsResDto;
import com.moli.iov.model.dto.req.PageExtend;
import com.moli.iov.rest.RestResponse;
import com.moli.iov.util.CommonUtils;
import com.moli.iov.util.ExcelUtil;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * The type Sys param settings controller.
 *
 * @author wanghualin
 * @date 2019-10-08 20:35:12
 */
@Slf4j
@RestController
@RequestMapping("/sys/sysParamSettings")
public class SysParamSettingsController extends BaseController {

  @Autowired
  private SysParamSettingsService listVehicleFence;
  /**
   * 导出设置参数列表
   */
  @PostMapping("/export")
  public RestResponse exportSysParamSettings(
      @Validated() @RequestBody SysParamSettingsQueryReqVo vo, HttpServletResponse response) {
    assembleUserInfo(vo);
    SysParamSettingsQueryReqDto dto = new SysParamSettingsQueryReqDto();
    CommonUtils.copyProperties(vo, dto);
    dto.setPageNum((long) 1);
    dto.setPageSize(Constants.EXPORT_PAGE_SIZE);
    RestResponse<PageExtend<SysParamSettingsResDto>> res = listVehicleFence
        .listSysParamSettings(dto);
    final List data = res.flag &&
        res.getData() != null &&
        res.getData().getRecords() != null ?
        res.getData().getRecords() :
        new ArrayList();
    String[] propNames = {"companyName", "paramName", "paramDesc", "paramValueStr"
        , "modifiedUser", "gmtModified"};
    String[] headStr = {"企业名称", "参数名称", "参数说明", "当前值", "最后编辑人", "最后编辑时间"};
    String title = "车机型号";
    try {
      ExcelUtil.exportDataExcel(request, response, data, propNames, headStr, title);
      return RestResponse.success();
    } catch (Exception e) {
      e.printStackTrace();
      return RestResponse.failure("导出异常");
    }
  }

  /**
   * 系统设置参数列表
   */
  @PostMapping("/list")
  public RestResponse listSysParamSettings(
      @Validated() @RequestBody SysParamSettingsQueryReqVo vo) {
    assembleUserInfo(vo);
    SysParamSettingsQueryReqDto dto = new SysParamSettingsQueryReqDto();
    CommonUtils.copyProperties(vo, dto);
    RestResponse<PageExtend<SysParamSettingsResDto>> iPageRestResponse = listVehicleFence
        .listSysParamSettings(dto);
    return iPageRestResponse;
  }

  /**
   * 添加系统设置参数
   */
  @PostMapping("/update")
  public RestResponse updateSysParamSettings(
      @Validated({SysParamSettingsReqVo.Update.class}) @RequestBody SysParamSettingsReqVo vo) {
    assembleUserInfo(vo);
    SysParamSettingsReqDto dto = new SysParamSettingsReqDto();
    CommonUtils.copyProperties(vo, dto);
    return listVehicleFence.updateSysParamSettings(dto);
  }


}
